<?php

/**
 * @author huynhtuvinh87
 * @copyright 2013
 */
class News extends CActiveRecord {

    private $_oldTags;

    /**
     * Returns the static model of the specified AR class.
     * @return CActiveRecord the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return '{{news}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('name,content', 'required'),
            array('create_time, category_id', 'numerical', 'integerOnly' => true),
            array('status, news', 'in', 'range' => array(0, 1, 2, 3)),
            array('alias', 'length', 'max' => '255'),
            array('tags', 'match', 'pattern' => '/^[\w\s,]+$/', 'message' => 'Tags can only contain word characters.'),
            array('tags', 'normalizeTags'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'category' => array(self::BELONGS_TO, 'NewsCategories', 'category_id'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'id' => 'Id',
            'name' => 'Tiêu đề',
            'image' => 'Hình minh họa',
            'category_id' => 'Danh mục',
            'create_time' => 'Ngày nhập',
            'content' => 'Nội dung',
            'state' => 'Tình trạng',
            'alias' => 'Đường dẫn',
            'status' => 'Trạng thái',
            'news' => 'Tin nổi bật'
        );
    }

    public function getTagLinks() {
        $links = array();
        foreach (Tag::string2array($this->tags) as $tag)
            $links[] = CHtml::link(CHtml::encode($tag), array('/product', 'tag' => $tag));
        return $links;
    }

    public function getDetail() {
        return  $this->category->alias . '/' . $this->alias;
    }

    /**
     * Normalizes the user-entered tags.
     */
    public function normalizeTags($attribute, $params) {
        $this->tags = Tag::array2string(array_unique(Tag::string2array($this->tags)));
    }

    protected function afterFind() {
        parent::afterFind();
        $this->_oldTags = $this->tags;
    }

    protected function afterSave() {
        parent::afterSave();
        Tag::model()->updateFrequency($this->_oldTags, $this->tags);
    }

    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id);
        $criteria->addSearchCondition('name', $this->name);
        $criteria->compare('status', $this->status);
        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
            'pagination' => array(
                'pageSize' => '20',
            ),
            'sort' => array(
                'defaultOrder' => 'id DESC'
            ),
        ));
    }

}
